/**
 * MenuHander.java
 * Handles the myriad menu options.
 * 
 * Copyright (c) 2012 The Javanauts
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package edu.utsa.cs4953.abacus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * Event handler for clicking a button.
 * 
 */
public class MenuHandler implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		// These are called "Yoda conditions", like if (5 == value)
		if ("About".equals(e.getActionCommand())) {
			String text = "";
			Scanner s;
			try {
				s = new Scanner(new File("About.html"));
				while (s.hasNextLine())
					text += s.nextLine();
				JOptionPane.showMessageDialog(null, new JLabel(text),
						"About Nepohualtzintzin",
						JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
								"images/mayazero.png"));
			} catch (FileNotFoundException e1) {
				System.err.println(e1.getMessage() + "About.html");
			}

		}
		if ("Quit".equals(e.getActionCommand())) {
			System.exit(0);
		}
		if ("Reset Board".equals(e.getActionCommand())) {
			Board.getInstance().Reset();
		}

		if ("Settings".equals(e.getActionCommand())) {
			Settings.getProgram().spawnSettingsWindow();
		}

		if (e.getActionCommand().equals("Toggle Decimal Numerals"))
			Settings.toggleStatusPanel();
		else if (e.getActionCommand().equals("Toggle Mayan Numerals"))
			Settings.toggleMayanNumerals();

		if (e.getActionCommand().equals("Toggle Split Mode")) {
			if (Settings.getMode() == Mode.NORMAL)
				Settings.setMode(Mode.SPLIT);
			else
				Settings.setMode(Mode.NORMAL);
		}

		if (e.getActionCommand().equals("Trivia"))
			Trivia.displayRandomTrivia();

		if (e.getActionCommand().equals("Addition"))
			Settings.setMode(Mode.NORMAL);
		else if (e.getActionCommand().equals("Multiplication"))
			Settings.setMode(Mode.SPLIT);

		if (e.getActionCommand().equals("Save")) {
			Settings.saveUserSettings();
			JOptionPane.showMessageDialog(null, "Settings saved.", "Settings",
					JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
							"images/mayazero_small.png"));
		}
		
		if (e.getActionCommand().equals("Sound Click"))
			Settings.setCurrentSoundEffect(SoundEffect.CLICK);
		else if (e.getActionCommand().equals("Sound Blip"))
			Settings.setCurrentSoundEffect(SoundEffect.BLIP);
		else if (e.getActionCommand().equals("Sound Off"))
			Settings.setCurrentSoundEffect(SoundEffect.NONE);

		if (e.getActionCommand().equals("Enable Trivia on Startup"))
			Settings.setShowTrivia(!Settings.isShowTrivia());

		Settings.getBoard().setNeedsResizing(true);
		Program.content.repaint();
	}
}